package com.briup.estore.service.impl;

import com.briup.estore.bean.ShopAddress;
import com.briup.estore.bean.ShopAddressExample;
import com.briup.estore.dao.ShopAddressMapper;
import com.briup.estore.service.ShopAddressService;
import com.briup.estore.util.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ShopAddressServiceImpl implements ShopAddressService {
    private ShopAddressMapper shopaddressMapper;
    {
        SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
        shopaddressMapper = sqlSession.getMapper(ShopAddressMapper.class);
    }
    @Override
    public List<ShopAddress> findAllByUserId(Integer userId) {
        ShopAddressExample example = new ShopAddressExample();
        example.createCriteria().andCustomerIdEqualTo(userId);
        return shopaddressMapper.selectByExample(example);
    }

    @Override
    public Integer addShopAddress(ShopAddress shopAddress) {
        shopaddressMapper.insert(shopAddress);
        // 使用mybatis-generator内置mapper插入之后可以直接获得新插入的数据id
        return shopAddress.getId();

    }

    @Override
    public ShopAddress findAddressById(Integer id) {
        return shopaddressMapper.selectByPrimaryKey(id);
    }
}
